//
// Created by dddgzc on 2025/1/24.
//

#ifndef DRAWUTILS_H
#define DRAWUTILS_H

#include "Camera.h"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>

extern SDL_Renderer* mainRenderer;

inline void draw(const Camera& camera,SDL_Texture* texture, int x, int y) {
    int textureWidth;
    int textureHeight;
    SDL_QueryTexture(texture, nullptr, nullptr, &textureWidth, &textureHeight);
    // 将宽高缩小一半
    SDL_Rect destRect = {static_cast<int>(x - camera.getPosition().x), static_cast<int>(y-camera.getPosition().y), textureWidth, textureHeight};
    SDL_RenderCopy(mainRenderer, texture, nullptr,&destRect);
}

// 裁剪绘制指定图片宽高
inline void drawSplit(const Camera& camera, SDL_Texture* texture, SDL_Rect& srcRect, SDL_Rect& destRect) {
    SDL_RenderCopy(mainRenderer, texture, &srcRect, &destRect);
}

inline void drawImage(const Camera& camera, SDL_Texture* texture, int x, int y) {
    SDL_RenderCopy(mainRenderer, texture, nullptr, nullptr);
}

// 加载图片
inline SDL_Texture* loadImage(const char* filename) {
    SDL_Surface* loadedSurface = IMG_Load(filename);
    SDL_Texture* texture = SDL_CreateTextureFromSurface(mainRenderer, loadedSurface);
    SDL_FreeSurface(loadedSurface);
    return texture;
}

inline void drawText(const Camera& camera, TTF_Font* font, const char* text, SDL_Color color, int x, int y) {
    SDL_Surface* surface = TTF_RenderText_Solid(font, text, color);
    if (surface == nullptr) {
        printf("无法绘制文本：%s\n", SDL_GetError());
        return;
    }
    SDL_Texture* texture = SDL_CreateTextureFromSurface(mainRenderer, surface);
    int w, h;
    SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
    SDL_Rect rect = {x, y, w, h};
    SDL_RenderCopy(mainRenderer, texture, nullptr, &rect);
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
}

inline void drawRedLine(const Camera& camera, int y, int left, int right) {
    SDL_SetRenderDrawColor(mainRenderer, 255, 0, 0, 255);
    SDL_RenderDrawLine(mainRenderer, left, y, right, y);
}

// 绘制边框
inline void drawRectangle(const Camera& camera, int x, int y, int width, int height, SDL_Color color) {
    SDL_SetRenderDrawColor(mainRenderer, color.r, color.g, color.b, 255);
    SDL_Rect rect = {(int)(x - camera.getPosition().x), (int)(y - camera.getPosition().y), width, height};
    SDL_RenderDrawRect(mainRenderer, &rect);
}

// 绘制填充物
inline void drawRectangleFull(const Camera& camera, int x, int y, int width, int height, SDL_Color color) {
    SDL_SetRenderDrawColor(mainRenderer, color.r, color.g, color.b, 255);
    SDL_Rect rect = {(int)(x - camera.getPosition().x), (int)(y - camera.getPosition().y), width, height};
    SDL_RenderFillRect(mainRenderer, &rect);
}


#endif //DRAWUTILS_H
